<?php

namespace app\admin\controller;

use think\Db;

class Organization
{

    private $categories = array();
    private $links = array();
    private $organization = array();
    private $articleIdArr = array();
    public function getData()
    {
        $data = request()->put();
        $unit_name = $data['unit_name'];

        $map = array();
        if (!empty($unit_name)) {
            $map['unit_name'] = array('=', $unit_name);
        }

        $result = Db::table('zstp_neo4j_organization')
            ->where($map)
            ->find();

        if (empty($result)) {
            return json(array(
                'code' => 400,
                'message' => '机构不存在'
            ));
        }

        $this->articleIdArr[] = $result['article_id'];

        $eventArray = array();

        //查该机构下所有你事件
        //组织事件
        $map_organization_event = array();
        $map_organization_event['unit_name'] = array('=', $unit_name);
        $organization_event = Db::table('zstp_neo4j_significant_events')
            ->where($map_organization_event)
            ->select();

        foreach ($organization_event as $key => $value) {
            $eventArray[] = array(
                "type" => "zzsj",
                "date" => $value['event_date'],
                "title" => $value['event_title'],
                "description" => $value['description'],

            );
            $this->articleIdArr[] = $value['article_id'];
        }

        //组织历史变迁
        $map_organization_history = array();
        $map_organization_history['unit_name'] = array('=', $unit_name);
        $organization_history = Db::table('zstp_neo4j_historical_evolution')
            ->where($map_organization_history)
            ->select();

        foreach ($organization_history as $key => $value) {
            $eventArray[] = array(
                "type" => "zzlsbq",
                "date" => $value['change_date'],
                "title" => $value['change_type'],
                "description" => $value['before_change'] . "->" . $value['after_change'],
            );
            $this->articleIdArr[] = $value['article_id'];
        }

        //奖惩事件
        $map_organization_reward = array();
        $map_organization_reward['subject_name'] = array('=', $unit_name);
        $organization_reward = Db::table('zstp_neo4j_rewards_punishments_event')
            ->where($map_organization_reward)
            ->select();

        foreach ($organization_reward as $key => $value) {
            $eventArray[] = array(
                "type" => "jcsj",
                "date" => $value['event_date'],
                "title" => $value['event_type'],
                "description" => $value['reason'],
            );
            $this->articleIdArr[] = $value['article_id'];
        }
        //中心机构
        $organizationCenter = array(
            "id" => 1,
            "name" => $result['unit_name'],
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "zhongxin",
        );

        $this->organization[] = $organizationCenter;

        //机构信息
        $organizationInfo = array(
            "id" => 2,
            "name" => "机构信息",
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "grxx",
        );

        $this->organization[]  = $organizationInfo;

        $categories = array(
            "name" => "机构信息",

        );

        $this->categories[] = $categories;

        $links = array(
            "category" => $categories['name'],
            "source" => 1,
            "target" => 2,

        );

        $this->links[] = $links;


        //获取机构信息
        $organizationInfo = $this->getOrganizationInfo($result);

        $this->organizationEvent($organization_event);

        $this->organizationHistory($organization_history);
        $this->organizationReward($organization_reward);


        //相关人物
        $mapDepartment = array();
        $mapDepartment['department'] = array('=', $unit_name);
        $mapDepartment['is_delete'] = array('=', '0');
        $departmentList = Db::table('zstp_neo4j_position_event')
            ->where($mapDepartment)
            ->select();


        $this->listDepartment($departmentList);

        // $this->listPerson($personList);

        //相关档案

        $articleList = Db::table('zstp_article')
            ->where('id', 'in', $this->articleIdArr)
            ->field('id,dh,status')
            ->select();



        $this->listArticle($articleList);



        $neo4jData = array(
            "categories" => $this->categories,
            "links" => $this->links,
            "organization" => $this->organization,
        );

        return json(array(
            'code' => 200,
            'data' => array("organization" => $result, "events" => $eventArray, "neo4jData" => $neo4jData),
            'message' => 'success'
        ));
    }

    public function listDepartment($departmentList)
    {
        if (empty($departmentList)) {
            return;
        }

        $organizationInfo = array(
            "id" => 5000,
            "name" => "相关人物",
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "xgrw",
        );
        $this->organization[] = $organizationInfo;
        $categories = array(
            "name" => "相关人物",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "相关人物",
            "source" => 1,
            "target" => 5000,
        );
        $this->links[] = $links;

        foreach ($departmentList as $key => $value) {
            $id = "xgrw_" . $value["id"];
            $organizationInfo = array(
                "id" => $id,
                "name" => $value['personnel_name'],
                "type" => "organization",
                "color" => "#1296db",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" =>$value['event_type'] ,
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 5000,
                "target" => $id,
            );

            $this->links[] = $links;
        }
    }

    public function listArticle($articleList)
    {


        if (empty($articleList)) {
            return;
        }
        //

        $organizationInfo = array(
            "id" => 4000,
            "name" => "相关档案",
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "xgda",
        );
        $this->organization[] = $organizationInfo;
        $categories = array(
            "name" => "相关档案",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "相关档案",
            "source" => 1,
            "target" => 4000,
        );
        $this->links[] = $links;

        foreach ($articleList as $key => $value) {
            $id = "xgda_" . $value["id"];
            $organizationInfo = array(
                "id" => $id,
                "name" => $value['dh'],
                "type" => "document",
                "color" => "#9fd1d8",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "档案",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "档案",
                "source" => 4000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    public function organizationReward($organization_reward)
    {
        if (empty($organization_reward)) {
            return;
        }

        $organizationInfo = array(
            "id" => 3000,
            "name" => "相关奖惩事件",
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "jc",
        );
        $this->organization[] = $organizationInfo;
        $categories = array(
            "name" => "相关奖惩事件",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "相关奖惩事件",
            "source" => 1,
            "target" => 3000,
        );
        $this->links[] = $links;

        foreach ($organization_reward as $key => $value) {
            $id = "jc_" . $value["id"];
            $organizationInfo = array(
                "id" => $id,
                "name" => $value['summary'],
                "type" => "organization",
                "color" => "#46a6ff",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => $value['event_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 3000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    //获取机构历史变迁
    public function organizationHistory($organization_history)
    {

        if (empty($organization_history)) {
            return;
        }

        $organizationInfo = array(
            "id" => 2000,
            "name" => "历史沿革",
            "type" => "organization",
            "color" => "#fac455",
            "symbol" => "zzlsbq",
        );
        $this->organization[] = $organizationInfo;

        $categories = array(
            "name" => "相关历史沿革",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "机构历史变迁",
            "source" => 1,
            "target" => 2000,
        );
        $this->links[] = $links;

        foreach ($organization_history as $key => $value) {
            if (!empty($value['change_type'])) {
                $id = "zzlsbq_" . $value["id"];
                $organizationInfo = array(
                    "id" => $id,
                    "name" => $value['before_change'] . "->" . $value['after_change'],
                    "type" => "organization",
                    "color" => "#f26667",
                    "symbol" => "circle",
                );
                $this->organization[] = $organizationInfo;

                $categories = array(
                    "name" => $value['change_type'],
                );
                $this->categories[] = $categories;

                $links = array(
                    "category" => $value['change_type'],
                    "source" => 2000,
                    "target" => $id,
                );
                $this->links[] = $links;
            }
        }
    }
    //获取机构事件

    public function organizationEvent($organization_event)
    {

        if (empty($organization_event)) {
            return;
        }

        $organizationInfo = array(
            "id" => 1000,
            "name" => "相关事件",
            "type" => "organization",
            "color" => "#green",
            "symbol" => "xgsj",
        );
        $this->organization[] = $organizationInfo;
        $categories = array(
            "name" => "相关事件",

        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "相关事件",
            "source" => 1,
            "target" => 1000,
        );

        $this->links[] = $links;


        foreach ($organization_event as $key => $value) {


            if (!empty($value['description'])) {

                $id = "se_" . $value["id"];
                $organizationInfo = array(
                    "id" =>  $id,
                    "name" => $value['description'],
                    "type" => "organization",
                    "color" => "#fac455",
                    "symbol" => "circle",
                );
                $this->organization[] = $organizationInfo;

                $categories = array(
                    "name" => $value['event_title'],
                );
                $this->categories[] = $categories;

                $links = array(
                    "category" => $value['event_title'],
                    "source" => 1000,
                    "target" => $id,
                );
                $this->links[] = $links;
            }
        }
    }
    //获取机构信息
    function getOrganizationInfo($organization)
    {

        // var_dump($organization);

        if ($organization['unit_designation'] != "") {
            $organizationInfo = array(
                "id" => 3,
                "name" => $organization['unit_designation'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "组织番号",

            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "组织番号",
                "source" => 2,
                "target" => 3,
            );

            $this->links[] = $links;
        }

        if ($organization['parent_unit_name'] != "") {
            $organizationInfo = array(
                "id" => 4,
                "name" => $organization['parent_unit_name'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "上级机构",

            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "上级机构",
                "source" => 2,
                "target" => 4,
            );
            $this->links[] = $links;
        }

        if ($organization['unit_type'] != "") {
            $organizationInfo = array(
                "id" => 5,
                "name" => $organization['unit_type'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "机构类型",

            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "机构类型",
                "source" => 2,
                "target" => 5,
            );
            $this->links[] = $links;
        }

        if ($organization['commander_name'] != "") {
            $organizationInfo = array(
                "id" => 6,
                "name" => $organization['commander_name'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "指挥官",

            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "指挥官",
                "source" => 2,
                "target" => 6,
            );
            $this->links[] = $links;
        }

        if ($organization['location'] != "") {
            $organizationInfo = array(
                "id" => 7,
                "name" => $organization['location'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "驻地",

            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "驻地",
                "source" => 2,
                "target" => 7,
            );
            $this->links[] = $links;
        }

        if ($organization['area_of_responsibility'] != "") {
            $organizationInfo = array(
                "id" => 8,
                "name" => $organization['area_of_responsibility'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "责任区",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "责任区",
                "source" => 2,
                "target" => 8,
            );
            $this->links[] = $links;
        }

        if ($organization['strength'] != "") {
            $organizationInfo = array(
                "id" => 9,
                "name" => $organization['strength'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "编制人数",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "编制人数",
                "source" => 2,
                "target" => 9,
            );
            $this->links[] = $links;
        }

        if ($organization['specialization'] != "") {
            $organizationInfo = array(
                "id" => 10,
                "name" => $organization['specialization'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "专业",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "专业",
                "source" => 2,
                "target" => 10,
            );
            $this->links[] = $links;
        }

        if ($organization['note'] != "") {
            $organizationInfo = array(
                "id" => 11,
                "name" => $organization['note'],
                "type" => "organization",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->organization[] = $organizationInfo;
            $categories = array(
                "name" => "备注",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "备注",
                "source" => 2,
                "target" => 11,
            );
            $this->links[] = $links;
        }
    }
}
